//
//  NEDTableViewController.m
//  MoreCoin
//
//  Created by 林宁宁 on 2019/11/14.
//  Copyright © 2019 MoreCoin. All rights reserved.
//

#import "NEDTableViewController.h"
#import "NEDConstant.h"
#import "UIColor+NEDStyle.h"
#import <Masonry/Masonry.h>

@interface NEDTableViewController ()

@end

@implementation NEDTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:(UITableViewStylePlain)];
    self.tableView.rowHeight = Adapted(60);
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [UIView new];
    self.tableView.tableHeaderView = [UIView new];
    self.tableView.separatorColor = [UIColor ned_separator];
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.estimatedRowHeight = 0;
    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 60, 0);
    [self.view addSubview:self.tableView];
    
    [self.tableView registerClass:[NEDTableViewCell class] forCellReuseIdentifier:[NEDTableViewCell cellIndentify]];
    
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return MAX(self.groupDataList.count, 1);
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(section < self.groupDataList.count){
        return self.groupDataList[section].count;
    }
    return 0;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NEDTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:[NEDTableViewCell cellIndentify]];
    cell.showAccessoryArrowView = YES;
    NEDBaseCellItem * item = self.groupDataList[indexPath.section][indexPath.row];
    if(item.cellShowBlock){
        item.cellShowBlock(cell, item);
    }
    cell.textLabel.text = item.title;
    cell.detailTextLabel.text = item.subTitle;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if(indexPath.section < self.groupDataList.count &&
       indexPath.row < self.groupDataList[indexPath.section].count){
        NEDBaseCellItem * item = self.groupDataList[indexPath.section][indexPath.row];
        if([item isKindOfClass:[NEDBaseCellItem class]]){
            if(item.clickBlock){
                item.clickBlock(item);
            }
        }
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.01;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}

-(NSMutableArray *)dataList{
    if (!_dataList) {
        _dataList = @[].mutableCopy;
        
        [self.groupDataList removeAllObjects];
        [self.groupDataList addObject:_dataList];
    }
    return _dataList;
}

-(NSMutableArray<NSMutableArray *> *)groupDataList{
    if (!_groupDataList) {
        _groupDataList=@[].mutableCopy;
    }
    return _groupDataList;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
